//
//  STMServiceIMViewController.m
//  StoreManagement
//
//  Created by centling on 16/6/7.
//  Copyright © 2016年 alfred. All rights reserved.
//

#import "STMServiceIMViewController.h"
#import <JavaScriptCore/JavaScriptCore.h>

@protocol JSObjcDelegate <JSExport>

- (void)close;
- (NSString *)getSellerInfo;

@end


@interface STMServiceIMViewController ()<UIWebViewDelegate, JSObjcDelegate>

@property (nonatomic, strong) JSContext *jsContext;
@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) NSString *url;


@end

@implementation STMServiceIMViewController

- (void)webViewDidStartLoad:(UIWebView *)webView {
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSString *urlStr = [request.URL absoluteString];
    if (![urlStr isEqualToString:self.url]) {
        STMServiceIMViewController *serviceIMViewController = [[STMServiceIMViewController alloc] init];
        serviceIMViewController.url = urlStr;
        [self.navigationController pushViewController:serviceIMViewController animated:YES];
        return NO;
    }
    return YES;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
    self.webView.backgroundColor = [UIColor whiteColor];
    self.webView.delegate = self;
    [self.view addSubview:self.webView];
    self.jsContext = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    self.jsContext[@"im"] = self;
    self.jsContext.exceptionHandler = ^(JSContext *context, JSValue *exceptionValue) {
        context.exception = exceptionValue;
        NSLog(@"异常信息：%@", exceptionValue);
    };
    
    self.url = self.url?self.url:@"http://121.42.178.223/CoolFish/wap/tmpl/seller/chat_list.html";
    NSURL *url = [NSURL URLWithString:self.url];
    NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60];
    [self.webView loadRequest:request];
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 20, 44, 44)];
    button.backgroundColor = [UIColor clearColor];
    [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:button];
}

-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

#pragma mark -- JSObjcDelegate
- (void)close {
    dispatch_async(dispatch_get_main_queue(), ^{
        UINavigationController *nav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
        [nav popViewControllerAnimated:YES];
    });
}

- (NSString *)getSellerInfo {
    return [NSString stringWithFormat:@"%@,%@", [UserDefaultHandler getUserDetaultString:kMemberName], [UserDefaultHandler getUserDetaultString:kUserPwd]];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
